package com.sec.android.app.myfiles.widget.listview.listdecorator;

import android.content.Context;
import android.content.res.Resources;
import android.widget.GridView;
import android.widget.RelativeLayout;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.module.local.category.CategoryFileRecord;
import com.sec.android.app.myfiles.navigation.NavigationInfo;
import com.sec.android.app.myfiles.util.PreferenceUtils;
import com.sec.android.app.myfiles.util.UiUtils;
import com.sec.android.app.myfiles.widget.listview.AbsListViewImp;
import com.sec.android.app.myfiles.widget.listview.pinchview.PinchGridView;

/**
 * Created by jujube.lee on 2016-04-01.
 */
public class GridListPinchDecorator extends AbsListDecorator {
    private int[] mItemWidth;

    @Override
    public void setListInfo(Context context, AbsListViewImp listView, NavigationInfo navigationInfo) {
        super.setListInfo(context, listView, navigationInfo);
        Resources res = context.getResources();
        mItemWidth = new int[]{
                (int) res.getDimension(R.dimen.category_grid_two_item_size),
                (int) res.getDimension(R.dimen.category_grid_three_item_size),
                (int) res.getDimension(R.dimen.list_item_grid_size)};

        int depth = PreferenceUtils.getGridViewPinchDepth(mContext);
        int itemSpacing = (int) res.getDimension(R.dimen.category_grid_view_item_spacing);
        int sidePadding = (int) res.getDimension(R.dimen.category_grid_view_padding_side);
        int topPadding = (int) res.getDimension(R.dimen.category_grid_view_padding_top);

        mListView.setPadding(sidePadding, topPadding, sidePadding, topPadding);
        ((PinchGridView) mListView).setVerticalSpacing(itemSpacing);
        ((PinchGridView) mListView).setHorizontalSpacing(itemSpacing);
        ((PinchGridView) mListView).setColumnWidthList(mItemWidth);
        ((PinchGridView) mListView).setEnablePinchOperation(mIsPinchEnabled);
        ((PinchGridView) mListView).setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
        ((PinchGridView) mListView).setPinchDepth(depth);

        UiUtils.setMaxPinchDepth(PinchGridView.MAX_PINCH_DEPTH);
        if (mNavigationInfo.getStorageType() == FileRecord.StorageType.Category) {
            FileRecord record = mNavigationInfo.getCurRecord();
            if (UiUtils.isCategoryFolderList(record)) {
                UiUtils.setMaxPinchDepth(PinchGridView.MAX_PINCH_DEPTH - 1);
                ((PinchGridView) mListView).setPinchDepth(depth);
            }
        }
        setTabletLayoutMargin(context);
    }

    private void setTabletLayoutMargin(Context context) {
        RelativeLayout view = (RelativeLayout) mListView.getParent();
        if (view != null) {
            RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) view.getLayoutParams();
            lp.topMargin = context.getApplicationContext().getResources().getDimensionPixelSize(R.dimen.grid_view_top_margin);
        }
    }

    @Override
    public int getListLayoutId() {
        return R.id.grid_list;
    }

    @Override
    public int getViewStubId() {
        return R.id.grid_list_stub;
    }

    @Override
    public int getItemLayoutId() {
        if (mNavigationInfo.getStorageType() == FileRecord.StorageType.Category) {
            FileRecord record = mNavigationInfo.getCurRecord();
            if (UiUtils.isCategoryFolderList(record)) {
            /* only when 1 depth */
                return R.layout.category_folder_grid_item;
            }
        }
        return R.layout.category_grid_item;
    }

    @Override
    public void stopPinchZoomWorking() {
        ((PinchGridView) mListView).clearAnimationInfo();
    }

    @Override
    public void refreshLayout() {
        if (mAbsListView.getFragment() != null && mAbsListView.getFragment().getNavigationInfo() != null) {
            FileRecord record = mAbsListView.getFragment().getNavigationInfo().getCurRecord();
            if (record instanceof CategoryFileRecord && ((CategoryFileRecord) record).getCategoryType() == FileRecord.CategoryType.Video) {
                mAbsListView.getAdapter().reload();
            }
        }
        updateItemWidth();
    }

    private void updateItemWidth() {
        Resources res = mContext.getResources();
        mItemWidth = new int[]{
                (int) res.getDimension(R.dimen.category_grid_two_item_size),
                (int) res.getDimension(R.dimen.category_grid_three_item_size),
                (int) res.getDimension(R.dimen.list_item_grid_size)};
        ((PinchGridView) mListView).setColumnWidthList(mItemWidth);
    }
}
